#include <iostream>
#include <fstream>
#include <string>
#include "DocParser.h"

using namespace std;
using namespace mosqoo::util;

int main()
{
    string url;
    cout << "URL:";
    cin >> url;
    if (url.substr(url.length() - 1, url.length()) != "\\" || url.substr(url.length() - 1, url.length()) != "/")
    {
        url.append("/");
    }
    ifstream in;
    in.open((url+"in.txt").c_str(), ios::in);
    if ( ! in )
    {
        cout << "Error: Can't open the file named " << url << "in.txt" << endl;
        url.erase(0, url.length());
        url.append("d:/projects/data/");
        cout << "Try to read " << url << "in.txt" << endl;
        in.close();
        in.clear();
        in.open((url+"in.txt").c_str(), ios::in);
    }
    ofstream out;
    out.open((url+"out.txt").c_str(),ios::trunc & ios::out);
    cout << "Try to write to " << url << "out.txt" << endl;
    try
    {
        string s;
        while (getline(in, s))
        {
            cout << "Location: " << in.tellg() << endl;
            out << s << endl;
        }
    }
    catch(std::ifstream::failure e)
    {
        if(!in.eof())
            cout << e.what() <<endl;
    }

    in.close();
    out.close();

    string subject = "$MOVE#robot#100#\n,#100#haha\ntest$";
    string regex = "(?ms)(#[0-9]*#)";
    string key;
    vector<string> result;

    cout << "Subject String: " << subject << endl;
    cout << "Regular Expression: " << regex << endl;
    cout << endl <<"Search ..." << endl;

    DocParser *docP = new DocParser();
    key = docP->registerRegex(regex);
    result = docP->search(key,subject);

    if (result.size() > 0)
    {
        for (int i = 0; i < result.size(); i++)
        {
            cout << "Result " << i+1 << " : " << result[i] << endl;
        }
    }
    else
    {
        cout << "The system can NOT found requested strings" << endl;
    }

    delete docP;

    return 0;
}
